package com.t235.gc.biz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.biz.service.SortingService;
import com.t235.gc.bizvo.SortingListVo;
import com.t235.gc.entity.FinanceSorting;
import com.t235.gc.entity.SysManager;
import com.t235.gc.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description :
 * Created by Li on 2021-04-09 11:39:19
 */
@RestController
@RequestMapping("/api/manager/admin")
public class SortingController {
    @Autowired
    private SortingService sortingService;

    @GetMapping("/getManagerList")
    public AjaxResult getManagerList(SysManager sysManager, @RequestParam(name = "page" ,defaultValue = "0") Integer pageNum , @RequestParam(name = "limit",defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SortingListVo> sortingListVos = sortingService.getManagerList(sysManager);
        PageInfo<SortingListVo> pageInfo = new PageInfo<>(sortingListVos);
        return AjaxResult.success(pageInfo);
    }

    @PostMapping("/saveOrUpdateSorting")
    public AjaxResult UpdateSorting(SysManager sysManager) {
        sysManager.setPassword(MD5.md5(sysManager.getPassword()));
        if(sysManager.getManagerid()!=null && sysManager.getManagerid()!= ""){
            return  AjaxResult.success(sortingService.updateByPrimaryKeySelective(sysManager));
        } else {
            String id = Math.random() * 0.99 + "";
            id = id.substring(id.indexOf(".") + 1, 18);
            sysManager.setManagerid("MA"+id);
            sysManager.setManagertype(3);
            return AjaxResult.success(sortingService.insertSelective(sysManager));
        }

    }
    @DeleteMapping("/delManager")
    public AjaxResult delManager(String managerid) {
        return AjaxResult.success(sortingService.delManager(managerid));
    }

    @PostMapping("/addFinanceSorting")
    public AjaxResult addFinanceSorting(FinanceSorting financeSorting) {
        return AjaxResult.success(sortingService.addFinanceSorting(financeSorting));
    }
}
